﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using Messenia.Data;
using System.IO;
using System.Threading;
using System.Collections.ObjectModel;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Diagnostics;
using System.Runtime.Remoting;


namespace Messenia.GUI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Socket m_clientSocket;
        private delegate void mydel(List<projects_list> pl);
        TcpChannel tcpChannel;


        public Window1()
        {
            InitializeComponent();
            tcpChannel = new TcpChannel();
            ChannelServices.RegisterChannel(tcpChannel,false);
            remoting();            
            ////projects p = new projects();
            ////p.Project_Name = "proj2";
            ////p.Project_Descr = "descr2";
            ////me.projects.AddObject
            ////me.SaveChanges();            
            //using (var context = new MesseniaEntities())
            //{
            //    listView1.ItemsSource = context.projects_list;

            //    projectstatus ps = (from s in context.projectstatus                          
            //                                  select s).First();

            //    humanresources us = (from s in context.humanresources
            //                        select s).First();



            //    //Create a product and a relationship to a known category by ID
            //    //projects p = new projects
            //    //{
            //    //    Project_CreationDate = new DateTime(2010,5,2,12,23,12),
            //    //    Project_Descr = "Bovril",
            //    //    Project_Name = "13",
            //    //    users = us,
            //    //    projectstatus = ps
            //    //};
            //    ////Add the product (and create the relationship by FK value)
            //    //context.AddObject("projects", p);

            //    //context.SaveChanges();
            //}

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ////getData();

            ////listView1.ItemsSource = me.projects;
            ////listView1.DisplayMemberPath = "Project_Descr";

            ////comboBox1.ItemsSource = me.groups;
            ////comboBox1.DisplayMemberPath = "Group_Name";
            //StartClient();
            DisableGrid();

        }

        private void remoting()
        {
            Thread tr = new Thread(new ThreadStart(LoadProj));
            tr.Start();
        }

       

        private void LoadProj()
        {

            RemotingConfiguration.Configure ("Messenia.GUI.exe.config", false);


            MesseniaDataManager remoteObject = remoteObject.GetInstance();
            //Activator.GetObject(typeof(MesseniaDataManager),
            //"tcp://localhost:7195/GetDataManager");
            //MesseniaDataManager dataManager = remoteObject.GetInstance();
            
            //this.Dispatcher.Invoke(new mydel(dowork), dataManager.GetProjects());
        }

    

      
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Add af = new Add(this);
            //af.ShowDialog();
            //ASCIIEncoding encoder = new ASCIIEncoding();
            //byte[] buffer = encoder.GetBytes("Hello Server!");
            //m_clientSocket.Send(buffer);
            //Thread t = new Thread( new ThreadStart(load_projects));
            //t.Start();
            listView1.ItemsSource = "";

            //listView1.ItemsSource = oc_projects_list;            
        }

        private void load_projects()
        {
            //Projects p = new Projects();
            //p.getProjects();
            //while (!p.Project_Loaded) ;
            //this.Dispatcher.Invoke(new mydel(dowork), p.Projects_list);


        }

        private void dowork(List<projects_list> list)
        {
            listView1.ItemsSource = list;
            canvas.Visibility = Visibility.Hidden;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //var sel = listView1.SelectedItem as projects_list;

            //using (var context = new MesseniaEntities())
            //{
            //    projects pr = (from s in context.projects                          
            //                   where s.Project_ID==sel.ID
            //                            select s).First();
            //    context.DeleteObject(pr);

            //    context.SaveChanges();
            //    listView1.ItemsSource = context.projects_list;

            //}      
            DisableGrid();
        }

        private void listView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show((listView1.SelectedItem as projects_list).Name);
        }

        private void DisableGrid()
        {

            Rectangle blockingRectangle = new Rectangle();
            blockingRectangle.Width = listView1.ActualWidth;
            blockingRectangle.Height = listView1.ActualHeight;            
            // Create a SolidColorBrush and use it to
            // paint the rectangle.
            SolidColorBrush myBrush = new SolidColorBrush(Colors.Gray);
            blockingRectangle.Stroke = Brushes.Black;
            blockingRectangle.StrokeThickness = 1;
            blockingRectangle.Fill = myBrush;
            blockingRectangle.Opacity = 0.1;
            blockingRectangle.Margin = listView1.Margin;
            canvas.Children.Insert(0, blockingRectangle);
        }
    }
    public class BooleanToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value,
                              Type targetType,
                              object parameter,
                              System.Globalization.CultureInfo culture)
        {
            if (value is int)
            {
                int a = (int)value;
                switch (a)
                {
                    case 1:
                        return false;
                    case 2:
                        return false;
                    case 3:
                        return false;
                    default:
                        return false;
                       
                }
            }
            return "Error";
        }

        public object ConvertBack(object value,
                                  Type targetType,
                                  object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            return null;
        }

        #endregion
    }
   

}
